package com.db.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.db.common.pojo.PageObject;
import com.db.dao.SysRoleDao;
import com.db.dao.SysRoleMenuDao;
import com.db.pojo.SysRole;
import com.db.service.SysRoleService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public PageObject<SysRole> findObjects(String userName, Long pageCurrent) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("modifiedTime");
        if (!StringUtils.isEmpty(userName)) {
            queryWrapper.like("username", userName);
        }

        IPage<SysRole> page = new Page<>(pageCurrent, 7);
        IPage<SysRole> sysRoleIPage = sysRoleDao.selectPage(page, queryWrapper);

        return new PageObject<SysRole>(sysRoleIPage.getCurrent(), (int) sysRoleIPage.getSize(), sysRoleIPage.getTotal(), sysRoleIPage.getPages(), sysRoleIPage.getRecords());
    }

    @Override
    public void insertObject(SysRole sysRole, Integer[] menuIds) {
        sysRoleDao.insert(sysRole);

        //删除角色和菜单关系表数据
//        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
//        wrapper.eq("role_id", sysRole.getId());
//        sysRoleMenuDao.delete(wrapper); //根据角色id删除

        //新增角色和菜单关系表数据
        sysRoleMenuDao.insertObject(sysRole.getId(), menuIds);
    }
}
